package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.JwtToken;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.SetmealDishesMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.dto.setmealDto.SetmealAddDto;
import com.itheima.pojo.dto.setmealDto.SetmealDishes;
import com.itheima.pojo.dto.setmealDto.SetmealUpdateDto;
import com.itheima.pojo.vo.setmealVo.SetmealByCategoryIdVo;
import com.itheima.pojo.vo.setmealVo.SetmealByIdVo;
import com.itheima.pojo.vo.setmealVo.SetmealByPageVo;
import com.itheima.pojo.vo.setmealVo.SetmealDishesVo;
import com.itheima.service.SetmealService;
import com.itheima.common.AuthThreadLocal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishesMapper setmealDishesMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public boolean add(SetmealAddDto setmeal) {
        String id = IdUtil.getSnowflakeNextIdStr();
        int result2 = 0;

        setmeal.setId(id);

        setmeal.setCreateUser(JwtToken.parse(AuthThreadLocal.getAuth()));

        setmeal.setCode("1");
        int result1 = setmealMapper.add(setmeal);

        if (result1 > 0) {


            for (SetmealDishes dishes : setmeal.getSetmealDishes()) {


                    dishes.setSetmealId(id);
                    result2 = setmealDishesMapper.add(dishes);
                    result2 += result2;
                }



        }
        return result1 * result2 > 0;
    }

    @Override
    public boolean update(SetmealUpdateDto setmeal) {

        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(JwtToken.parse(AuthThreadLocal.getAuth()));
        int result = 0;
        String id = setmeal.getId();
        int update = setmealMapper.update(setmeal);
        for (SetmealDishes setmealDish : setmeal.getSetmealDishes()) {
            setmealDish.setSetmealId(id);

            result = setmealDishesMapper.update(setmealDish);
            result += result;
        }
        return update * result > 0;
    }

    @Override
    public boolean delete(String[] ids) {
        int result = 0;
        int result2 = 0;
        for (String id : ids) {
            result = setmealDishesMapper.delete(id);
            result2 = setmealMapper.delete(id);
            result += result;
            result2 += result2;

        }
        return result * result2 > 0;
    }

    @Override
    public List<SetmealByPageVo> selectByPage(String name, Integer page, Integer pageSize) {
        page = (page - 1) * pageSize;
        List<SetmealByPageVo> list = new ArrayList<>();

        List<SetmealByPageVo> setmealByIdVos = setmealMapper.selectByPage(name, page, pageSize);

        for (SetmealByPageVo setmeal : setmealByIdVos) {
            List<SetmealDishesVo> dishes = setmealDishesMapper.selectById(setmeal.getId());
            setmeal.setSetmealDishes(dishes);
            list.add(setmeal);

        }

        return list;
    }

    @Override
    public boolean status(int num, String[] ids) {
        int result = 0;
        for (String id : ids) {

            SetmealUpdateDto setmeal = new SetmealUpdateDto();
            setmeal.setId(id);
            setmeal.setStatus(num);
            result += setmealMapper.update(setmeal);

        }
        return result > 0;
    }

    @Override
    public SetmealByIdVo selectById(String id) {

        SetmealByIdVo setmealByIdVo = setmealMapper.selectById(id);
        String name = categoryMapper.findName(setmealByIdVo.getCategoryId());

        List<SetmealDishesVo> dishesList = setmealDishesMapper.selectById(id);
        setmealByIdVo.setSetmealDishes(dishesList);
        setmealByIdVo.setCategoryName(name);
        return setmealByIdVo;
    }



    @Override
    public List<SetmealByCategoryIdVo> selectAllByCategoryId(String categoryId, Integer status) {
        return setmealMapper.selectAllByCategoryId(categoryId);
    }
}
